package chances.epg.data.taglib;

import javax.servlet.jsp.JspException;

import org.slf4j.Logger;

import chances.epg.commons.log.EpgLogFactory;
import chances.epg.entity.biz.Biz;
import chances.epg.navigator.context.AccessContext;
import chances.epg.navigator.context.AccessContextUtils;


public class BizTag extends QueryTag {
	private static final Logger logger = EpgLogFactory.getSystemLogger();

	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = 3399601666905725793L;
	private String code;

	@Override
	public int doEndTag() throws JspException {
		String bizCode = this.getCode();

		try {
			/*
			if (StringUtils.isBlank(bizCode)) {
				this.setResult(this.getCurBiz());
			} else {
				Biz biz = this.getBizService().getBizByCode(bizCode);
				this.setBizAccessURL(biz);
				this.setResult(biz);
			}
			*/
			Biz biz = this.getBizService().getBizByCode(bizCode);
			this.setBizAccessURL(biz);
			this.setResult(biz);
		} catch (Throwable ex) {
			logger.error("BizTag error", ex);
		}

		return super.doEndTag();
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	private void setBizAccessURL(Biz biz) {
		AccessContext context = AccessContextUtils
				.getAccessContext(pageContext);
		if (biz != null) {
			biz.setUrl(navigateUrlFactory.generateURL(context, biz));
		}  
	}
}
